(*
  Copyright (c) 2009 Barry Schwartz

  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*)

(* A |builder 'a| can be used to create a |list 'a| by successively appending elements
   to the end of the list. A list [x1;...; xn] is represented by the value [t; x1;...; xn]
   where <t> is a pointer to the last element of the list. *)
type 'a builder = 'a list

(* |make ()| creates a new list builder. O(1) *)
let make () = let rec b = [ b ] in Obj.magic b
  
(*** private helper functions ***)
(* |tail <b>| returns the tail pointer and |list <b>| the current list. *)
let tail b = Obj.magic (List.hd b)
  
let list b = List.tl b
  
(* |set_field <x> <y> <n>| sets the <n>th field of the block <x> to <y>. *)
let set_field x y n = Obj.set_field (Obj.repr x) n (Obj.repr y)
  
(* |set_tail <b> <x>| and |set_list <b> <x>| set, respectively, the tail pointer
   and the current list to <x>. *)
let set_tail b x = set_field b x 0
  
let set_list b x = set_field b x 1
  
let set_last b x = set_field (tail b) x 0
  
(* |append_to_tail <b> <l>| appends the list <l> to <b> without changing
   the tail pointer. *)
let append_to_tail b l = Obj.set_field (Obj.repr (tail b)) 1 (Obj.repr l)
  
(* |append_builders <b1> <b2>| appends the contents of <b2> to <b1>. <b2> is not modified.
   It is assumed that <b2> is non-empty and different from <b1>. *)
let append_builders b1 b2 =
  (append_to_tail b1 (list b2); set_tail b1 (tail b2))
  
(*** public functions ***)
(* |is_empty <b>| checks whether the current list is still empty.
   O(1), pure *)
let is_empty b = match list b with | [] -> true | _ -> false
  
(* |clear <b>| empties the builder. O(1), non-pure *)
let clear b = (set_tail b b; append_to_tail b [])
  
(* |add <b> <x>| adds the element <x> at the end of the current list.
   |add_first <b> <x>| adds <x> to the front. O(1), non-pure *)
let add b x = let n = [ x ] in (append_to_tail b n; set_tail b n)
  
let add_first b x =
  if is_empty b
  then (* If the list is empty we also need to update the tail pointer. *)
    add b x
  else set_list b (x :: (list b))
  
(* |add_list <b> <x>| adds all elements of the list <x> to the end of the current
   list. O(x), non-pure *)
let rec add_list b lst =
  match lst with | [] -> () | x :: xs -> (add b x; add_list b xs)
  
(* |append <b1> <b2>| appends the contents of <b2> to <b1> and resets <b2>.
   O(1), non-pure *)
let append b1 b2 =
  match list b2 with
  | [] -> ()
  | _ ->
      if ( != ) b1 b2
      then (append_builders b1 b2; clear b2)
      else invalid_arg "ListBuilder.append: equal arguments not permitted!"
  
(* |get <b>| returns the current list and resets the builder. O(1), non-pure *)
let get b = let l = list b in (clear b; l)
  
(* |get_last <b>| returns the last element of the current list. O(1), pure *)
let get_last b =
  match tail b with
  | [] -> invalid_arg "ListBuilder.get_last: empty list!"
  | _ -> List.hd (tail b)
  
(* |replace_last <b> <x>| replaces the last element of the current list with <x>. *)
let replace_last b x =
  match list b with
  | [] -> invalid_arg "ListBuilder.replace_last: empty list!"
  | _ -> set_last b x
  

